package ac.biu.cs.textcat2011.coloreddoc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Code to create HTML files based on given clusters
 * @author Asher Stern
 * @since Aug 18, 2011
 *
 */
public class DocCreator
{
	public static final String TEXT_MARKER = "#";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			int index=0;
			if (args.length<(index+1))throw new RuntimeException("args");
			String vectorFileName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String assignmentFileName = args[index];
			++index;
			if (args.length<(index+1))throw new RuntimeException("args");
			String htmlFileName = args[index];
			++index;
			
			DocCreator app = new DocCreator(new File(assignmentFileName),new File(vectorFileName),new File(htmlFileName));
			app.go();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	public DocCreator(File assignmentsFile, File vectorFile, File htmlFile)
	{
		super();
		this.assignmentsFile = assignmentsFile;
		this.vectorFile = vectorFile;
		this.htmlFile = htmlFile;
	}



	public void go() throws IOException
	{
		BufferedReader assignmentReader = new BufferedReader(new FileReader(assignmentsFile));
		try
		{
			BufferedReader vectorReader = new BufferedReader(new FileReader(vectorFile));
			try
			{
				List<SequenceOfWordsAndCluster> list =  createClusteredSequences(assignmentReader, vectorReader);
				createHtmlFile(list);
			}
			finally{try{vectorReader.close();}catch(Exception e){}}
			
		}
		finally{try{assignmentReader.close();}catch(Exception e){}}
		
		
	}
	
	private void createHtmlFile(List<SequenceOfWordsAndCluster> list) throws FileNotFoundException
	{
		int major = findMajor(list);
		PrintWriter writer = new PrintWriter(htmlFile);
		try
		{
			for (SequenceOfWordsAndCluster item : list)
			{
				if (item.getCluster()!=major)
				{
					writer.print("<font color=\"red\">");
				}
				writer.print(item.getSequenceOfWords());
				if (item.getCluster()!=major)
				{
					writer.print("</font>");
				}
				writer.println();
			}
		}
		finally{if (writer!=null)try{writer.close();}catch(Exception e){}}
	}
	
	private int findMajor(List<SequenceOfWordsAndCluster> list)
	{
		int zeroCount = 0;
		int oneCount = 0;
		for (SequenceOfWordsAndCluster item : list)
		{
			if (0==item.getCluster())
				zeroCount++;
			else if (1==item.getCluster())
				oneCount++;
			else throw new RuntimeException("Illegal item with cluster = "+item.getCluster());
		}
		if (zeroCount>oneCount)
			return 0;
		else
			return 1;
	}
	
	private List<SequenceOfWordsAndCluster> createClusteredSequences(BufferedReader assignmentReader, BufferedReader vectorReader) throws IOException
	{
		List<SequenceOfWordsAndCluster> list = new LinkedList<SequenceOfWordsAndCluster>();
		String assignmentLine = null;
		String vectorLine = null;
		
		assignmentLine = assignmentReader.readLine();
		vectorLine = vectorReader.readLine();
		while ( (assignmentLine!=null) && (vectorLine!=null) )
		{
			int cluster = getAssignment(assignmentLine);
			String text = getText(vectorLine);
			
			list.add(new SequenceOfWordsAndCluster(text,cluster));
			
			assignmentLine = assignmentReader.readLine();
			vectorLine = vectorReader.readLine();
		}
		if ( (assignmentLine!=null) || (vectorLine!=null) )
		{
			throw new RuntimeException("(assignmentLine!=null) || (vectorLine!=null)");
		}
		
		ArrayList<SequenceOfWordsAndCluster> returnValue = new ArrayList<SequenceOfWordsAndCluster>(list.size());
		returnValue.addAll(list);
		return returnValue;
	}
	
	private int getAssignment(String assignmentLine)
	{
		int ret = Integer.parseInt(assignmentLine);
		if ( (ret!=0) && (ret != 1) )
			throw new RuntimeException("assignment = "+ret);
		
		return ret;
	}
	
	private String getText(String vectorLine)
	{
		int textMarker = vectorLine.indexOf(TEXT_MARKER);
		if (textMarker<0)throw new RuntimeException("Illegal line :"+vectorLine);
		String ret = vectorLine.substring(textMarker+1).trim();
		if (ret.length()==0)throw new RuntimeException("Empty text");
		return ret;
	}
	
	
	private File assignmentsFile;
	private File vectorFile;
	private File htmlFile;
}
